package com.caimiao.miao.page.master;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.caimiao.miao.R;
import com.caimiao.miao.interfaces.BasePresenterImpl;
import com.caimiao.miao.page.master.advert.AdvertFragment;
import com.caimiao.miao.page.master.device.DeviceFragment;
import com.caimiao.miao.page.master.information.InformationFragment;
import com.caimiao.miao.page.master.storage.StorageFragment;
import com.caimiao.miao.page.master.system.SystemFragment;
import com.caimiao.miao.page.master.ticket.TicketFragment;

public class MasterPresenter extends BasePresenterImpl<IMasterContact.View, IMasterContact.Model> implements IMasterContact.Presenter {
    public MasterPresenter(IMasterContact.View mView) {
        super(mView);
    }

    @Override
    protected IMasterContact.Model getModel() {
        return new MasterModel();
    }

    @Override
    public void start(Context context) {
    }

    @Override
    public void replaceFragment(FragmentManager fm, int id) {

        switch (id) {
            case R.id.masterTvDevice:
                setFragment(fm, DeviceFragment.class, false);
                break;
            case R.id.masterTvInfo:
                setFragment(fm, InformationFragment.class, false);
                break;
            case R.id.masterTvStorage:
                setFragment(fm, StorageFragment.class, false);
                break;
            case R.id.masterTvTicket:
                setFragment(fm, TicketFragment.class, false);
                break;
            case R.id.masterTvAdvert:
                setFragment(fm, AdvertFragment.class, false);
                break;
            case R.id.masterTvSystem:
                setFragment(fm, SystemFragment.class, false);
                break;
        }
    }

    @Override
    public <T> void setFragment(FragmentManager fm, Class<T> tClass, boolean canBack) {
        String TAG = tClass.getSimpleName();
        Fragment fragment = fm.findFragmentByTag(TAG);
        boolean isCreate = false;
        if (fragment == null) {
            try {
                fragment = (Fragment) tClass.newInstance();
                isCreate = true;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        mView.replace(fragment, isCreate, TAG, canBack);
    }

}
